import api from '../../utils/api'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    labels:[],
    submitData:{
      grades:0,//星级
      activeid:'',//活动id
      hf_name:'',
      hf_phone:'',
      ishf:0,
      labelid:[]
    },
    datainfo:'',
    title:''
  },
  //提交评论
  async submit(){
    let data= this.data.submitData
    if( data.grades==0 ){
      Toast("请为本活动评星")
      return
    }
    if( data.labelid.length==0 ){
      Toast('请选择标签')
      return
    }
    if( data.labelid.length>3 ){
      Toast('最多选择3个标签')
      return
    }
    if(data.ishf==1){
      if ( !data.hf_name ) {
				Toast("请输入姓名")
				return
			}
			if ( !data.hf_phone ) {
				Toast("请输入手机号")
				return
			}
    }
    data['flag'] = 'activity_qtcomment'
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      //返回上一级关闭当前页面
      Toast.success('提交成功')
      wx.navigateBack({
        delta: 1
      })
    }else {
      Toast(message)
    }  
  },

  //是否成为青年观察员
  onChange1({ detail }) {
    // 需要手动对 checked 状态进行更新
    console.log(detail)
    this.data.submitData.ishf = detail?1:0
    this.setData({ submitData:this.data.submitData });
    if( detail ){
    this.gethfinfo()
    this.getCinfo()
    }
  },

  async gethfinfo(){
    let _this = this
    let data= {}
    data['flag'] = 'activity_commentgethfinfo'
    data['activeid'] = this.data.submitData.activeid
    const {recode, flag, hf_name, hf_phone, message} = await api.send(data)
    if (recode == 'SUCC') {
      if (flag == '1') {
        _this.data.submitData.hf_name = hf_name
        _this.data.submitData.hf_phone = hf_phone
      } else {
        _this.data.submitData.hf_name = ''
        _this.data.submitData.hf_phone = ''
      }
      _this.setData({submitData:_this.data.submitData})
    }

  },

  //获取阅读信息
	async getCinfo () {
		let sdata = {}
		sdata['flag'] = "activity_qtcomment_gcinfo"
    sdata['activeid'] = this.data.submitData.activityid
    const {recode, title, info, message} = await api.send(sdata)
    if (recode == 'SUCC') {
      let info1 = info
      info1 = info1.replace(/<p>/g,'').replace(/<\/p>/g,'\n')
      Dialog.alert({
        title: title,
        message: info1,
        theme: 'round-button',
        confirmButtonText:'知道了'
      }).then(() => {
        // on close
      });
    }
  },
  
  //评价内容
  selected(e){
    if( this.data.datainfo.flag==0 ) return

    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id;

    let b = this.data.labels
    if(b[index].selected == 1){
      b[index].selected = 0
    }else{
      b[index].selected = 1
    }
    let str = []
    b.forEach((item)=>{
      if( item.selected ) str.push(item.id)
    })
    this.data.submitData.labelid = str;
    this.setData({
      labels: b,
      submitData:this.data.submitData
    })
  },
  //评星
  onChange(e){
    this.data.submitData.grades = e.detail;
    this.setData({
      submitData:this.data.submitData
    })
    this.getdata()
  },
  onNameChange(e){
    this.data.submitData.hf_name = e.detail;
    this.setData({
      submitData:this.data.submitData
    })
  },
  onPhoneChange(e){
    this.data.submitData.hf_phone = e.detail;
    this.setData({
      submitData:this.data.submitData
    })
  },

  getdatainfo(){
    let _this = this
    let data= {}
    data['flag'] = 'activity_qtcomment_info'
    data['activeid'] = this.data.submitData.activeid
    data['grades'] = this.data.submitData.grades
    api.send(data, {
      message: '正在发送...', isloading: false, method:'post'
    }).then(resp => {
      if (resp.recode == 'SUCC') {
        console.log(resp)
        resp['flag'] = (resp['flag'] == 'ypl'?0:1)
        _this.setData({datainfo:resp,submitData:this.data.submitData})
        let title = ''
        if (resp.flag == 1) {
          title = '请为本次活动评星'
          if( resp.is_qt==1 ) title = '签退成功，' + title
          _this.setData({title})
					_this.getdata();
				} else {
					_this.info();
				}
      }
    }, fail => {
      console.log(fail);
    })  
  },

  info: function () {
		/*
		 grades
		 "labelid" : [
		 {"id":"","name":"","type":"是否被选中 1选中 0未选中"}
		 ],
		 "hf" : "0 不回访 1回访",
		 "hf_name" : "姓名",
		 "hf_phone" : "手机号"
		 */
		let datas = this.data.datainfo
		let stars = parseInt(datas.grades);
	  this.data.submitData.grades = stars
    this.data.labels = datas.labelid
    this.data.labels.forEach((item)=>{
      item.selected = 1
    })
    this.data.submitData.ishf = datas.is_hf
		if (datas.is_hf == '1') {
			let phone = datas.hf_phone;
      let newphone = phone.substr(0, 3) + "****" + phone.substr(7);
      this.data.submitData.hf_phone = newphone
      this.data.submitData.hf_name = datas.hf_name
    }
    this.setData({submitData:this.data.submitData,labels:this.data.labels})
	},

  async getdata () {
    let sdata= {}
    sdata['flag'] = 'activity_commentlabel'
    sdata['grades'] = this.data.submitData.grades
		if (parseInt(sdata['grades']) == 0) {
			sdata['grades'] = 5;
		} 
    const {recode, data, message} = await api.send(sdata)
    if(recode === 'SUCC') {
       this.setData({labels:data})
    }else {
      Toast(message)
    }  
	},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id||''
    if( options.scene ){
      let obj = api.decodeurl(options.scene)
      if( obj ) id = obj.id||''
    }
    this.data.submitData.activeid = id
    this.setData({
      submitData:this.data.submitData
    })
    this.getdatainfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})